package tw.com.hitevision.whiteboard.android.render;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Region;
import android.util.Log;

import java.util.ArrayList;
import tw.com.hitevision.whiteboard.android.state.WhiteboardState;
import tw.com.hitevision.whiteboard.android.stateFile.GroupStateFile;

public class Group extends BaseItem {

    ArrayList<BaseItem> member;

    public Group(){
        member = new ArrayList<BaseItem>();
    }


    @Override
    public void scaleFromPoint(float rateX, float rateY, PointF p) {
        for(BaseItem item : member){
            item.scaleFromPoint(rateX,rateY,p);
        }
        updateAllBounds();
    }

    @Override
    public void rotateFromPoint(float degree, PointF p) {
        for(BaseItem item : member){
            item.rotateFromPoint(degree,p);
        }
        updateAllBounds();
    }

    @Override
    public void moveBy(float dx, float dy) {
//        super.moveBy(dx, dy);
        for(BaseItem item : member){
            item.moveBy(dx,dy);
        }
        updateAllBounds();
    }

    @Override
    public BaseItem copy() {
        Group temp = new Group();
        for(BaseItem item : member){
            temp.addToGroup(item.copy());
        }
        temp.setBounds(getLeft(),getTop(),getRight()-getLeft(),getBottom()-getTop());
        return temp;
    }

    public static BaseItem copyFromStateFile(float x, float y, float width, float height, ArrayList<BaseItem> member) {
        Group temp = new Group();

        for(BaseItem item : member){
            temp.addToGroup(item.copy());
        }
        temp.setBounds(x, y, width, height);
        return temp;
    }

    public GroupStateFile getGroupStateFile() {
        return new GroupStateFile(this, member);
    }

    @Override
    public void render(Canvas canvas, Paint paint, Context context) {
        if(member.size()==0)
            return;
        for(BaseItem item : member){
            item.render(canvas, paint, context);
        }
    }


    public void addToGroup(BaseItem item){
        member.add(item);
//        if(member.size() == 1){
//            setBounds(item.getLeft(),item.getRight(),item.getTop(),item.getBottom());
//        } else {
//            updateBounds(item.getLeft(),item.getRight(),item.getTop(),item.getBottom());
//        }
    }

    public void updateBounds(float left, float right, float top, float bottom){
        float mleft = getLeft();
        float mright = getRight();
        float mtop = getTop();
        float mbottom = getBottom();

        Log.d("Group", "updateBounds: left:"+left+",right:"+right+",bottom:"+bottom+",top:"+top);

        mleft = Math.min(left,mleft);
        mright = Math.max(right, mright);
        mtop = Math.min(top,mtop);
        mbottom = Math.max(bottom,mbottom);

//        if(left<mleft)
//            mleft = left;
//        if(right > mright)
//            mright = right;
//        if(top < mtop)
//            mtop = top;
//        if(bottom > mbottom)
//            mbottom = bottom;

        setBounds(mleft, mtop, mright - mleft, mbottom - mtop);
    }

    public void updateAllBounds(){
        for( int i=0; i < member.size(); i++){
            BaseItem item = member.get(i);
            if(i == 0){
                setBounds(item.getLeft(),item.getTop(),item.getRight()-item.getLeft(),item.getBottom()-item.getTop());
            }else {
                updateBounds(item.getLeft(),item.getRight(),item.getTop(),item.getBottom());
            }
        }
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        for(BaseItem item:member){
            item.setSelected(selected);
        }
    }

    @Override
    public void getRegion(Region out) {
        Region temp = new Region();
        Region collector = new Region();
        for(BaseItem item:member){
            item.getRegion(collector);
            temp.op(collector, Region.Op.UNION);
        }
        out.set(temp);

    }


    public void ungroup(WhiteboardState whiteboardState){
        // todo: finish this part         

    }

}
